package com.supplychainapi.controller;

import com.alibaba.fastjson.JSONObject;
import com.joindata.inf.common.basic.exceptions.ServiceException;
import com.supplychainapi.common.ApiVersion;
import com.supplychainapi.common.RestResponse;
import com.supplychainapi.common.enums.ExceptionMessage;
import com.supplychainapi.domain.LoginCache;
import com.supplychainapi.param.CheckExceptParam;
import com.supplychainapi.proxy.ValidateHelper;
import com.supplychainapi.service.TbCheckExceptService;
import com.supplychainapi.utils.PathUitls;
import io.swagger.annotations.*;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by panic .
 * Time 2017/11/20
 * description:
 */


@RequestMapping(PathUitls.ROOT_PHONE + "/{version}/checkExcept")
@RestController
@Api(value = "CheckExceptController", description = "验收异常")
public class CheckExceptController extends BaseController {
    private Logger logger = LogManager.getLogger(this.getClass());
    @Autowired
    TbCheckExceptService checkExceptService;

    /**
     * 新增验收异常
     *
     * @param loginCache
     * @param model
     * @param request
     * @return
     * @throws ServiceException
     */
    @ApiVersion(1)
    @ApiOperation(value = "新增验收异常", notes = "")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "fsCheckExceptName", value = "验收异常名称;swagger", required = true, dataType = "String", defaultValue = ""),
    })
    public RestResponse save(@ApiIgnore LoginCache loginCache, @ApiIgnore @RequestBody CheckExceptParam model,
                             HttpServletRequest request) throws ServiceException {
        model.setFiStatus((short) 1)
                .setFsShopGUID(loginCache
                        .getFsShopGUID())
                .setFsCreateUserId(loginCache.getFsUserId())
                .setFsCreateUserName(loginCache.getFsUserName());
        ValidateHelper.validate(model);
        return checkExceptService.save(model);
    }

    /**
     * 更新验收异常
     *
     * @param loginCache
     * @param model
     * @param request
     * @return
     * @throws ServiceException
     */
    @ApiVersion(1)
    @ApiOperation(value = "更新验收异常", notes = "")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "fsCheckExceptName", value = "验收异常原因;swagger", required = true, dataType = "String", defaultValue = ""),
            @ApiImplicitParam(paramType = "query", name = "fiStatus", value = "启用禁用;swagger", required = false, dataType = "String", defaultValue = "1"),
            @ApiImplicitParam(paramType = "query", name = "fsCheckExceptId", value = "验收原因id;swagger", required = false, dataType = "String", defaultValue = ""),
    })
    public RestResponse update(@ApiIgnore LoginCache loginCache, @ApiIgnore @RequestBody CheckExceptParam model,
                               HttpServletRequest request) throws ServiceException {
        model.setFsShopGUID(loginCache.getFsShopGUID())
                .setFsUpdateUserId(loginCache.getFsUserId())
                .setFsUpdateUserName(loginCache.getFsUserName());
        ValidateHelper.validate(model);
        return checkExceptService.update(model);
    }

    /**
     * 删除验收异常
     *
     * @param loginCache
     * @param json
     * @param request
     * @return
     * @throws ServiceException
     */
    @ApiVersion(1)
    @ApiOperation(value = "删除验收异常", notes = "")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "list", value = "验收异常代码list;swagger",
                    required = true, dataType = "list", defaultValue = "")
    })
    public RestResponse delete(@ApiIgnore LoginCache loginCache, @ApiParam("code list") @RequestBody JSONObject json,
                               HttpServletRequest request) throws ServiceException {
        if (null == json || !json.containsKey("list")) {
            throw ExceptionMessage.missing_param.getServiceException();
        } else {
            List list = json.getJSONArray("list");
            return checkExceptService.delete(list, loginCache.getFsShopGUID());
        }
    }


    /**
     * 根据主键查询验收异常原因
     *
     * @param loginCache
     * @param fsCheckExceptId
     * @return
     * @throws ServiceException
     */

    @ApiVersion(1)
    @ApiOperation(value = "根据主键查询验收异常原因", notes = "")
    @RequestMapping(value = "/select", method = RequestMethod.GET)
    public RestResponse select(@ApiIgnore LoginCache loginCache,
                               @ApiParam("主键Id") @RequestParam String fsCheckExceptId) throws ServiceException {
        return checkExceptService.selectById(loginCache.getFsShopGUID(), fsCheckExceptId);
    }


    /**
     * 查询所有验收异常原因
     *
     * @param loginCache
     * @return
     * @throws ServiceException
     */

    @ApiVersion(1)
    @ApiOperation(value = "查询所有验收异常原因", notes = "")
    @RequestMapping(value = "/selectAll", method = RequestMethod.GET)
    public RestResponse selectAll(@ApiIgnore LoginCache loginCache) throws ServiceException {
        return checkExceptService.selectAll(loginCache.getFsShopGUID());
    }


    /**
     * 查询所有正在使用的验收异常原因
     *
     * @param loginCache
     * @return
     * @throws ServiceException
     */

    @ApiVersion(1)
    @ApiOperation(value = "查询所有验收异常原因", notes = "")
    @RequestMapping(value = "/selectActivateAll", method = RequestMethod.GET)
    public RestResponse selectActivateAll(@ApiIgnore LoginCache loginCache) throws ServiceException {
        return checkExceptService.selectActivateAll(loginCache.getFsShopGUID());
    }
}
